import uiautomator2 as u2
import random
from main import ADBHelper
import time

class Amas:

    def __init__(self):
        self.adb = ADBHelper()
        self.device = u2.connect()

    @staticmethod
    def timeit(func):
        def wrapper(*args, **kwargs):
            start = time.time()
            result = func(*args, **kwargs)
            end = time.time()
            print(f'方法 {func.__name__} 耗时: {end - start:.2f}秒')
            return result
        return wrapper
        
    def init(self):
        self.adb.init()
        # self.device_info = self.device.info

    def tap(self, x, y):
        print(f'点击坐标: {x}, {y}')
        self.device.click(x, y)
        self.device.touch.up(x, y)

    def click(self, x, y):
        print(f'点击坐标: {x}, {y}')
        self.device.click(x, y)

    def swipe(self, x1, y1, x2, y2, duration=500):
        print(f'滑动坐标: {x1}, {y1} -> {x2}, {y2}')
        self.adb.swipe(x1, y1, x2, y2, duration)

    def slide_up(self):
        self.swipe(0.5, 0.15, 0.5, 0.85)

    def slide_down(self):
        self.swipe(0.5, 0.85, 0.5, 0.15)

    def random_offset(self, n: int, offset: int) -> int:
        """
        对数字进行随机偏移处理
        :param n: 原始数字
        :param offset: 最大偏移量
        :return: 偏移后的结果
        """
        delta = random.randint(0, offset)
        if random.choice([True, False]):
            return n + delta
        return n - delta

    def random_float_offset(self, n: float, offset: int) -> float:
        """
        对浮点数进行随机偏移处理
        :param n: 原始数字（浮点类型）
        :param offset: 最大偏移量（整数）
        :return: 偏移后的结果（实际偏移量为offset/1000）
        """
        delta = random.randint(0, offset) / 1000
        if random.choice([True, False]):
            return n + delta
        return n - delta

    def offset_coordinates(self, x, y, offset):
        """
        对坐标进行随机偏移处理
        :param x: 原始x坐标
        :param y: 原始y坐标
        :param offset: 最大偏移量
        :return: (x_new, y_new) 偏移后的新坐标
        """
        x = self.random_float_offset(x, offset) if 0 < x < 1 else self.random_offset(x, offset)
        y = self.random_float_offset(y, offset) if 0 < y < 1 else self.random_offset(y, offset)
        return x, y

    @timeit
    def wait_xpath_click(self, xpath_str, timeout=20.0):
        element = self.device.xpath(xpath_str)
        if element.wait(timeout=timeout):
            x, y = element.get().center()
            self.tap(x, y)

    @timeit
    def wait_text_tag(self, text, timeout=20.0):
        element = self.device(text=text) 
        if element.wait(timeout=timeout):
            x, y = element.center()
            self.tap(x, y)

    @timeit
    def wait_text_click(self, text, timeout=20.0):
        element = self.device(text=text)
        element.click(timeout=timeout)

    @timeit
    def test(self):
        # print(self.device.xpath('//*[@text="设置"]').get().center()) # 获取中心坐标
        # print(self.device.xpath('//*[@text="设置"]').get().info['bounds']) # 获取边界坐标
        # self.device.xpath('//*[@text="设置"]').wait(timeout=10.0).click()  # 等待元素加载
        # print(self.device.xpath('//*[@text="设置"]').exists)
        # self.device.xpath('//*[@text="设置"]')
        # print(self.device(text='设置').center())
        self.slide_up()
        self.slide_down()

    @timeit
    def minicap_screenshot(self):
        self.adb.capture_screenshot()

    @timeit
    def automator_screenshot(self):
        self.device.screenshot()


if __name__ == '__main__':
    amas = Amas()
    amas.init()
    # amas.wait_xpath_click('//*[@text="设置"]')
    # amas.wait_text_click('设置')
    amas.test()

